﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyLibary.DAL;

namespace MyLibary.BIZ
{
    public class BIZ_ChiTietBan
    {
        #region Fields
        public string MaHDBan { get; set; }
        public string MaSanPham { get; set; }
        public decimal DonGia { get; set; }
        public int SoLuong { get; set; }
        public BIZ_HoaDonBan HDBan
        {
            get { return DAL_HoaDonBan.GetHoaDonBan(this.MaHDBan); }
        }
        public BIZ_SanPham SanPham
        {
            get { return DAL_SanPham.GetById(this.MaSanPham); }
        }
        #endregion

        #region Contructor
        public BIZ_ChiTietBan() { }
        public BIZ_ChiTietBan(string MaHDBan, string MaSanPham, decimal DonGia, int SoLuong)
        {
            this.MaHDBan = MaHDBan;
            this.MaSanPham = MaSanPham;
            this.DonGia = DonGia;
            this.SoLuong = SoLuong;
        }
        #endregion

        #region Metholds
        public static List<BIZ_ChiTietBan> GetAll(string MaHDBan)
        {
            return DAL_ChiTietBan.getAll(MaHDBan);
        }
        public static BIZ_ChiTietBan getByID(string MaHDBan,string MaSanPham)
        {
            return DAL_ChiTietBan.getByID(MaHDBan, MaSanPham);
        }
        public static decimal getTongTien(String MaHDBan)
        {
            return DAL_ChiTietBan.getTongTien(MaHDBan);
        }
        public bool Add()
        {
            bool success= DAL_ChiTietBan.Insert(this);
            if (success)
            {
                BIZ_HoaDonBan HDB = DAL_HoaDonBan.GetHoaDonBan(this.MaHDBan);
                HDB.TongTien = DAL_ChiTietBan.getTongTien(HDB.MaHDBan);
                HDB.Update();
            }
            return success;
        }
        public bool Update()
        {
           bool success= DAL_ChiTietBan.Update(this);
           if (success)
           {
               BIZ_HoaDonBan HDB = DAL_HoaDonBan.GetHoaDonBan(this.MaHDBan);
               HDB.TongTien = DAL_ChiTietBan.getTongTien(HDB.MaHDBan);
               HDB.Update();
           }
           return success;
        }
        public bool Delete()
        {
            bool success= DAL_ChiTietBan.Delete(this);
            if (success)
            {
                //BIZ_HoaDonBan HDB = DAL_HoaDonBan.GetHoaDonBan(this.MaHDBan);
                //HDB.TongTien = DAL_ChiTietBan.getTongTien(HDB.MaHDBan);
                //HDB.Update();
            }
            return success;
        }
        #endregion

        public class IEQualityCompareChiTietBan : IEqualityComparer<BIZ_ChiTietBan>
        {
            #region  IEQualityCompareChiTietBan
            public bool Equals(BIZ_ChiTietBan x, BIZ_ChiTietBan y)
            {
                return x.MaHDBan == y.MaHDBan && x.MaSanPham == y.MaSanPham;
            }
            public int GetHashCode(BIZ_ChiTietBan Object) { return Object.GetHashCode(); }
            #endregion
        }
    }
}
